<?php
class PhieuhangController extends Vnf_Controller_Action {
    function init() {

        $this->view->maximizeWin = true; // maximize window after popup
        $this->view->winWidth = 640; // default width of window
        $this->view->winHeight = 480; // default height of window
        parent::init();

        $this->setLayout('content');

        $this->view->translate = new Zend_Translate('tmx', BASE_DIR . '/translate/admin.tmx', 'en');

        $this->view->currentController = $this->_request->getControllerName();
        $this->view->objName = "Phieuhang"; // class name of Model
        $objName = $this->view->objName;
        $this->view->objModel = $obj = new $objName();

        $this->view->pk = $obj->getPrimaryKey();
    }

    public function indexAction() {
//        Zend_Layout::getLayout('content');
    }

    public function listAction() {
        $obj = $this->view->objModel;

        //$records = $obj->fetchAll();
        $records = $obj->getAll();
        $records = $records->toArray();

        $total = count($records);

        $results = array(
            'total' => $total,
            'results' => $records
        );

        echo Zend_Json::encode($results);die;
    }

    public function listAllAction() {
        $obj = $this->view->objModel;

        $records = $obj->fetchAll();
        $records = $records->toArray();

        $total = count($records);

        $results = array(
            'total' => $total,
            'results' => $records
        );

        echo Zend_Json::encode($results);die;
    }

    public function deleteAction() {
        $id = (int)$this->getRequest()->getParam('id', -1);
        $result = array();
        if($id > 0) {
            $obj = $this->view->objModel;
            $pk = $obj->getPrimaryKey();

            $obj->delete("$pk = '$id'");
            $result['success'] = true;
        }

        $strResult = Zend_Json::encode($result);
        echo $strResult;die;
    }

    public function saveAction() {
        $pk = $this->view->pk;

        $id = (int)$this->getRequest()->getParam($pk, -1);

        $params = $this->_request->getParams();
        $data = array(
            'MaSoPhieuHang' => @$params['MaSoPhieuHang'],
            'NguoiLapPhieuHang' => @$params['NguoiLapPhieuHang'],
            'NguoiLayHang' => @$params['NguoiLayHang'],
            'NgayLapPhieuHang' => date('Y-m-d', strtotime(@$params['NgayLapPhieuHang'])), //@$params['NgayLapPhieuHang'],
            'NgayGiaoHang' => date('Y-m-d', strtotime(@$params['NgayGiaoHang'])), // @$params['NgayGiaoHang'],
            'LoaiPhieuHang' => @$params['LoaiPhieuHang'],
            'SanPhamID' => @$params['SanPhamID'],
            'SoLuong' => @$params['SoLuong'],
        );

        $obj = $this->view->objModel;
        $result = array();
        $result['success'] = false;
        if ($id > 0) { // update
            try {
                $obj->update($data, "$pk = '$id'");

                $LoaiPhieuHang = -1;
                if ($data['LoaiPhieuHang'] == 0) {
                    $LoaiPhieuHang = 1;
                }
                $soLuongCu = $data['SoLuong'];
                $soLuongCu = @$params['SoLuongCu'];
                $soLuong = $LoaiPhieuHang * ($data['SoLuong'] - $soLuongCu);
                $sanPham = new Sanpham();
                $sanPham->updateSoLuong($data['SanPhamID'], $soLuong);


                $result['success'] = true;
                $result['message'] = $this->view->translate->_("Save successfull.");
            } catch (Exception $ex){
                $result['success'] = false;
                $result['message'] = $this->view->translate->_("Can't save to DB.") . "<br>" . $ex->getMessage();
            }
        } else { // add
            try {
                $obj->insert($data);

                $LoaiPhieuHang = -1;
                if ($data['LoaiPhieuHang'] == 0) {
                    $LoaiPhieuHang = 1;
                }
                $soLuong = $LoaiPhieuHang * $data['SoLuong'];
                $sanPham = new Sanpham();
                $sanPham->updateSoLuong($data['SanPhamID'], $soLuong);

                $result['success'] = true;
                $result['message'] = $this->view->translate->_("Insert successfull.");
            } catch(Exception $ex) {
                $result['message'] = $this->view->translate->_("Can't insert to DB") . "<br>" . $ex->getMessage();
            }
        }
        $strResult = Zend_Json::encode($result);
        echo $strResult;die;
    }
}